package com.xzt.meeting.controller;

import com.xzt.meeting.service.HomeService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xzt
 * @Date 2022/5/9 10:36
 * @Version 1.0
 * @Introduce
 */

@WebServlet("/home/*")
public class HomeServlet extends BaseServlet{

    private HomeService homeService = new HomeService();
    public void index(HttpServletRequest req, HttpServletResponse resp){
        // 查询 今日新注册用户、新发布文章、新发布会议等
        Map<String, Object> countData = homeService.findHomeCount();

        // 查询近7日新注册用户、新发布文章、新发布会议
        List<Map<String, Object>> detailData = homeService.findHomeDetail();

        // 转换为集合方式便于前端取出
        List<List> countList = new ArrayList<>();

        for (Map<String, Object> map : detailData) {

            List temp = new ArrayList();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                temp.add(entry.getValue());
            }
            countList.add(temp);
        }

        // 封装成一个map写到前端
        Map<String, Object> data = new HashMap<>();

        // 今日数据
        data.put("count", countData);

        // 近七日数据
        data.put("detail", countList);

        // 写给前端
        writerObjToString(resp, data);
    }
}
